package com.demo;


import com.demo.util.MidUtil;
import org.junit.jupiter.api.Test;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.io.File;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.Sequence;
import javax.sound.midi.Sequencer;

public class test01 {
    private static File sound;
    private static Sequence seq;
    private static Sequencer midi;
    public static void Play(String filename)
    {
        try
        {
            sound = new File(filename);
            seq = MidiSystem.getSequence(sound);
            midi= MidiSystem.getSequencer();
            midi.open();
            midi.setSequence(seq);

            if(!midi.isRunning())
                midi.start();

        } catch (Exception ex) {
        }
    }
    public static void Stop()
    {
        if(midi.isRunning())
            midi.stop();

        if(midi.isOpen())
            midi.close();
    }

    @Test
    public void test1() throws InterruptedException {
        //音符
        String[] note = {"12317716","031200123316012155152523000123152067137017606711233200","031200123316012155152523000123152023277105671234352110554","3054325103453160565224330665355332552201236","5433431212345617156^143211177","505112523210231234327125077125231067167176101122343455554","54334312554","50511252321"};
        //音调
        String[] pitch= {"44443454","444444444443444433434344444444444433443443343344444444","444444444443444433434344444444444444433443334444434444444","4434444444444444444444444444444444444444444","44444444444444545444544444433","444444444444444444443444433443444433433444444444444444444","44444444444","44444444444"};
        //节拍
        String[] meter= {"3111244g","421542112114211112262118442112226211224211421121122844","421542111214211112262118442112226211112422311211312184211","4111142a211222621111211a1111211222112221122","8314222i22222211a222a22224444","4211833211a211211222112211112221121121121142111211111c211","8314222e211","4211833211e"};
        //组成
        String[] component = {"0","0","1","2","3","4","2","3","5","3","6","3","7"};
        //音色
        String tone = "2222222222222";
        String path="D:\\test.mid";
        try {
            String[] args = new String[] { "python", "D:\\java\\project\\music\\src\\main\\resources\\test.py",Arrays.toString(note),Arrays.toString(pitch),Arrays.toString(meter),Arrays.toString(component),tone,path};
            Process proc = Runtime.getRuntime().exec(args);// 执行py文件
            BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
            String line = null;
            while ((line = in.readLine()) != null) {
                System.out.println(line);
            }
            in.close();
            proc.waitFor();
        }catch (Exception e) {
            e.printStackTrace();
        }
        Thread.sleep(300);
        Play(path);
        long time = midi.getMicrosecondLength()/1000;

        try
        {
            Thread.sleep(time);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        Stop();

    }
    @Test
    void test4(){
        MidUtil.mid2Wav("D:\\test.mid","D:\\test.wav");
    }
}
